# NIXON Preferences Manager
# (c) 2006-2008 NIXON Development Corporation.
from alert import alertBox

class prefManager():
	prefList = ''
	
	def start(self, strFile):
		self.strFile = strFile
		try:
			self.prefList = open(self.strFile, 'r').read()
		except:
			self.savePrefs()

	def savePrefs(self):
		try:
			fileObj = open(self.strFile,'w')
			fileObj.write(self.prefList)
			fileObj.close()
		except Exception, e:
			if e.errno == 13:
				strDesc = 'The preferences file is read-only. Try resetting the preferences file by holding Control while Shuttle starts and then choosing Reset from the resulting dialog.'
			else:
				strDesc = str(e)
			dlg = alertBox(None, 'Could not save preferences to disk.', strDesc)

	def doPrefs(self, strPref, strReplace, blChange = False):
		prefPos = self.prefList.find(strPref)
		if prefPos <> -1:
			equalPos = self.prefList.find('=', prefPos + len(strPref))
			startPos = self.prefList.find('"', equalPos) + 1
			endPos = self.prefList.find('"', startPos + 1)

			if equalPos == -1:
				print 'Error 100 in prefManager: PRF parse error on preference "%s" (pos %i)' % (strPref, prefPos + len(strPref))
			else:
				if blChange == True:
					strBeg = self.prefList[:startPos]
					strEnd = self.prefList[endPos:]
					self.prefList = strBeg + str(strReplace) + strEnd
				else:
					strRet = self.prefList[startPos:endPos]
					return strRet
			
		else:
			self.prefList += '\n' + strPref + ' = "' + str(strReplace) + '"'
			return str(strReplace)

	def parsePref(self, str, blInt=False):
		if blInt:
			try:
				return int(str)
			except:
				return -1
		elif str == -1 or str.upper() == 'TRUE' or str == '-1':
			return True
		elif str == 0 or str.upper() == 'FALSE' or str == '0':
			return False
		else:
			return False

	def getPref(self, strPref, strDefault, blChange = False, int=False):
		return self.parsePref(self.doPrefs(strPref, strDefault, blChange), int)
		
# preferences window

import wx

class prefWindow(wx.Dialog):
	def __init__(self, prnt):
		wx.Dialog.__init__(self, prnt)
		self.Bind(wx.EVT_CLOSE, self.onClose)
		self.prnt = prnt
		self.tpSize = self.prnt.GetSize()
		
		lblBrowser = titleLabel(self, label='Browser View:')
		self.chkSnapSize = wx.CheckBox(self, label='Snap window size')
		self.chkAutoFocus = wx.CheckBox(self, label='Activate columns upon hover')
		
		lblBoxSize = wx.StaticText(self, label='Column width:')
		self.sldBoxSize = wx.Slider(self, value=prnt.boxWidth, minValue=120, maxValue=480)
		self.sldBoxSize.Bind(wx.EVT_SCROLL_CHANGED, self.onBoxSizeScroll)
		self.txtBoxSize = wx.TextCtrl(self, value=str(prnt.boxWidth), size=(36,-1), style=wx.TE_CENTRE)
		self.txtBoxSize.SetMaxLength(3)
		self.txtBoxSize.Bind(wx.EVT_TEXT, self.onTxtBoxSize)
		lblPixels = wx.StaticText(self, label='px')
		
		lblImagePreviews = wx.StaticText(self, label='Image Previews:')
		chkLocalPreviews = wx.CheckBox(self, label='Local')
		chkRemotePreviews = wx.CheckBox(self, label='Remote')
		
		wrapper = wx.BoxSizer(wx.VERTICAL)
		
		sizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
		
		sizer.Add(lblBrowser, 0, wx.ALIGN_RIGHT)
		sizer.Add(self.chkSnapSize, 0)
		
		sizer.Add((0,0))
		sizer.Add(self.chkAutoFocus, 0)
		
		sizer.Add((0,0))
		boxSizeSizer = wx.BoxSizer(wx.HORIZONTAL)
		boxSizeSizer.Add(lblBoxSize, 0, wx.ALIGN_CENTER_VERTICAL)
		boxSizeSizer.Add(self.sldBoxSize)
		boxSizeSizer.Add(self.txtBoxSize)
		boxSizeSizer.Add(lblPixels, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT, 5)
		sizer.Add(boxSizeSizer, 0, wx.ALIGN_RIGHT)
		
		sizer.Add((0,0))
		previewSizer = wx.BoxSizer(wx.HORIZONTAL)
		previewSizer.Add(lblImagePreviews, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 5)
		previewSizer.Add(chkLocalPreviews, 0, wx.RIGHT, 5)
		previewSizer.Add(chkRemotePreviews)
		sizer.Add(previewSizer, 0)
		
		wrapper.Add(sizer, 0, wx.ALL, 20)
		
		self.chkSnapSize.SetValue(self.prnt.prefs.getPref('main.snapWindowSize', True))
		self.chkAutoFocus.SetValue(self.prnt.prefs.getPref('main.autoFocus', True))
		
		self.SetSizer(wrapper)
		self.Fit()
		
	def onClose(self, event):
		blSnapSize = self.chkSnapSize.GetValue()
		self.prnt.snapSizes = blSnapSize
		self.prnt.prefs.doPrefs('main.snapWindowSize', str(blSnapSize), True)
		
		blAutoFocus = self.chkAutoFocus.GetValue()
		self.prnt.autoFocus = blAutoFocus
		self.prnt.prefs.doPrefs('main.autoFocus', str(blAutoFocus), True)
		
		self.prnt.prefs.doPrefs('browser.boxWidth', self.sldBoxSize.GetValue(), True)
		self.Destroy()
		
	def onTxtBoxSize(self, event):
		try:
			intVal = int(event.String)
		except:
			intVal = 120
			self.txtBoxSize.SetValue(str(intVal))
		finally:
			if intVal < 120:
				intVal = 120
			elif intVal > 480:
				intVal = 480 
				
			self.sldBoxSize.SetValue(intVal)
			self.adjustColumns(intVal)
		
	def onBoxSizeScroll(self, event):
		value = event.GetInt()
		self.txtBoxSize.SetValue(str(value))
		self.adjustColumns(value)
		
	def adjustColumns(self, value):		
		for p in (self.prnt.pnLocal, self.prnt.pnRemote):
			lenB = len(p.lstBrowsers)

			vsize = (lenB + 1) * value
			p.spBrowser.SetVirtualSize((vsize, -1))
			p.spInfo.SetMinSize((value, 1))
			
			for x in range(lenB):
				box = p.lstBrowsers[x]
				box.SetMinSize((value-1, -1))
				
			p.boxWidth = value
			p.spBrowser.Scroll(0,0)
			p.Refresh()
			
		self.prnt.boxWidth = value
		self.prnt.onFrameSize(None, size=self.tpSize[0])
		
class titleLabel(wx.StaticText):
	def __init__(self, prnt, label):
		wx.StaticText.__init__(self, prnt, -1, label)
		self.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD, False))